package com.ls.stack;

/**
 * @program: dataStructure
 * @author: lishun
 * @create: 2021-12-04 22:36
 **/

import lombok.Data;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 数组实现栈
 */
@Data
public class ArrayStack {
    private int data[];
    // 栈的顶部
    private int top = -1;

    public ArrayStack(int maxsize) {
        this.data = new int[maxsize];
    }

    // 栈满
    public boolean isFull(){
        return this.top== this.data.length-1;
    }
    // 栈空
    public boolean isEpmty(){
        return this.top ==-1;
    }

    // 入栈
    public void push(int value){
        if (isFull()){
            throw new RuntimeException("栈满");
        }
        top++;
        this.data[top] = value;
    }
    // 将栈顶的数据返回
    public int pop(){
        if (isEpmty()){
            throw new RuntimeException("栈空了");
        }
        return this.data[top--];
    }

    public void show(){
        System.out.println();
        if (isEpmty()){
            System.out.println("栈空");
        }
        for (int i = this.top;i>=0;i--){
            System.out.print(this.data[i]);
        }
    }
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(3);
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        String keyInput = "";
        while (flag){
            System.out.println("输入exit退出:");
            System.out.println("输入push压栈:");
            System.out.println("输入pop弹栈:");
            System.out.println("输入show展示:");
            keyInput = scanner.next();
            switch (keyInput){
                case "exit":
                    flag = false;
                    break;
                case "push":
                    System.out.println("输入弹栈值（整形）");
                    int value = scanner.nextInt();
                    arrayStack.push(value);
                    break;
                case "pop":
                    System.out.println("弹出"+arrayStack.pop());
                    break;
                case "show":
                    arrayStack.show();
                    break;
            }
        }
    }
}
